<?php

class SoapField
{

	/**
	 * The display label.
	 *
	 * @var string
	 */
	private $label = '';

	/**
	 * The type of field (enum, varchar etc.)
	 *
	 * @var string
	 */
	private $type = '';

	/**
	 * The fieldname
	 *
	 * @var unknown_type
	 */
	private $name = '';

	/**
	 * If it is an enum the optios are specified here.
	 *
	 * @var array
	 */
	private $options = array ();

	/**
	 * The default value of the enum.
	 *
	 * @var string
	 */
	private $defaultValue = '';

	/**
	 * 
	 */
	function __construct( array $properties )
	{
		$this->label = $properties[ 'label' ];
		$this->type = $properties[ 'type' ];
		$this->name = $properties[ 'name' ];
		
		if ( isset( $properties[ 'options' ] ) && count( $properties['options'] ) > 0 )
		{
			foreach( $properties[ 'options' ] as $option )
			{
				$this->options[ $option[ 'name' ] ] = $option[ 'value' ];
			}
			
			if( isset( $properties[ 'default_value' ] ) )
			{
				$this->defaultValue = $properties[ 'default_value' ];
			}
		}
	}

	public function getLabel()
	{
		return $this->label;
	}

	public function getType()
	{
		return $this->type;
	}

	public function getName()
	{
		return $this->name;
	}

	public function getOptions()
	{
		return $this->options;
	}

	public function getDefaultValue()
	{
		return $this->defaultValue;
	}

	public static function createFromArray( array $soapResultSet )
	{
		$fields = array ();

		foreach( $soapResultSet as $row )
		{
			
			$field = new SoapField( $row );
			$fields[ $field->getName() ] = $field;
		}
		
		return $fields;
	}

	public function __toString()
	{
		return $this->getLabel();
	}
}
